package cn.jinjimi.app.main;

import java.util.Arrays;
import java.util.List;

import bolts.Continuation;
import bolts.Task;
import cn.jinjimi.app.bean.Constants;
import cn.jinjimi.app.controller.AdvController;
import cn.jinjimi.app.controller.PageController;
import cn.jinjimi.app.entity.LinkBean;
import cn.jinjimi.app.entity.Notice;
import cn.jinjimi.base.util.LogUtil;
import cn.jinjimi.domain.data.dto.CommentList;
import cn.jinjimi.domain.data.dto.ConfigDTO;
import cn.jinjimi.domain.entity.ProductList;
import cn.jinjimi.domain.store.AdvStore;

/**
 *
 * @author zhenhua
 * @date 2018/1/31
 */

public class HomePresenter extends BasePresenter<HomePresenter.IViewController> {

    private static final String TAG = "HomePresenter";

    public HomePresenter(HomePresenter.IViewController viewController) {
        super(viewController);
    }

    public void reloadAll() {
        List<Task<Integer>> tasks = Arrays.asList(
                loadAdv(), loadEntrance(),
                loadCateProducts(), loadGoods(),
                loadComments()
        );
        Task.whenAllResult(tasks)
                .continueWith(new Continuation<List<Integer>, Void>() {
                    @Override
                    public Void then(Task<List<Integer>> task) throws Exception {
                        int result = Constants.Code.RES_SUCCESS;
                        if (task.isFaulted() || task.isCancelled()) {
                            result = Constants.Code.RES_ERROR;
                            LogUtil.d(TAG, task.getError().toString());
                        }
                        int failed = 0;
                        for (Integer res : task.getResult()) {
                            if (res != Constants.Code.RES_SUCCESS) {
                                failed++;
                            }
                        }
                        if (failed == task.getResult().size()) {
                            result = Constants.Code.RES_ERROR;
                        }
                        getViewController().onLoaded(result);
                        return null;
                    }
                }, Task.UI_THREAD_EXECUTOR);
    }

    public interface IViewController {
        void renderCateList(List<ProductList> data);
        void renderAdvs(List<LinkBean> data);
        void renderEntrance(List<LinkBean> data);
        void renderComments(CommentList comments);
        void renderGoods(ProductList list);
        void onLoading();
        void onLoaded(int res);
        void renderNotices(List<Notice> data);
        void renderLimit(ConfigDTO.LimitEntry limit);
        void renderRecommends(List<Notice> data);
    }

    /**
     *
     * @return
     */
    public Task<Integer> loadAdv() {
        return AdvController
                .getAdvList(AdvStore.INDEX)
                .onSuccess(new Continuation<List<LinkBean>, Integer>() {
                    @Override
                    public Integer then(Task<List<LinkBean>> task) throws Exception {
                        List<LinkBean> advs = task.getResult();
                        if (advs != null && advs.size() > 0) {
                            getViewController().renderAdvs(advs);
                        }
                        return Constants.Code.RES_SUCCESS;
                    }
                }, Task.UI_THREAD_EXECUTOR);
    }

    /**
     *
     * @return
     */
    public Task<Integer> loadCateProducts() {
        return PageController
                .requestCate()
                .onSuccess(new Continuation<List<ProductList>, Integer>() {
                    @Override
                    public Integer then(Task<List<ProductList>> task) throws Exception {
                        List<ProductList> data = task.getResult();
                        if (data != null) {
                            getViewController().renderCateList(data);
                        }
                        return Constants.Code.RES_SUCCESS;
                    }
                }, Task.UI_THREAD_EXECUTOR);
    }

    public Task<Integer> loadComments() {
        return PageController
                .requestComments()
                .onSuccess(new Continuation<CommentList, Integer>() {
                    @Override
                    public Integer then(Task<CommentList> task) throws Exception {
                        CommentList comments = task.getResult();
                        if (comments != null) {
                            getViewController().renderComments(comments);
                        }
                        return Constants.Code.RES_SUCCESS;
                    }
                }, Task.UI_THREAD_EXECUTOR);
    }

    public Task<Integer> loadEntrance() {
        return PageController
                .requestPageConfig()
                .onSuccess(new Continuation<ConfigDTO, Integer>() {
                    @Override
                    public Integer then(Task<ConfigDTO> task) throws Exception {
                        ConfigDTO config = task.getResult();
                        if (config != null) {
                            getViewController().renderEntrance(config.getEntrances());
                            getViewController().renderNotices(config.getNotices());
                            getViewController().renderLimit(config.getLimit());
                            getViewController().renderRecommends(config.getRecommends());
                        }
                        return Constants.Code.RES_SUCCESS;
                    }
                }, Task.UI_THREAD_EXECUTOR);

    }

    public Task<Integer> loadGoods() {
        return PageController
                .requestGoods()
                .onSuccess(new Continuation<ProductList, Integer>() {
                    @Override
                    public Integer then(Task<ProductList> task) throws Exception {
                        if (task.isCompleted()) {
                            ProductList list = task.getResult();
                            if (list != null) {
                                getViewController().renderGoods(list);
                            }
                        }
                        return Constants.Code.RES_SUCCESS;
                    }
                }, Task.UI_THREAD_EXECUTOR);
    }

}
